package com.wlyuan.open.scanner.application.task.order;

import com.wlyuan.open.scanner.assembler.ScannerOrderAssembler;
import com.wlyuan.open.scanner.constant.Constants;
import com.wlyuan.open.scanner.dao.mapper.OrderIndexMapper;
import com.wlyuan.open.scanner.domain.order.OrderConvert;
import com.wlyuan.open.scanner.repository.OrderRepository;
import com.wlyuan.open.scanner.repository.impl.BaseOrderRepository;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.util.StopWatch;

import java.util.concurrent.CountDownLatch;

/**
 * @author lijing
 * @description
 */
@Slf4j
@Data
public class OrderTask implements Runnable {
    private int pageNum;
    private int pageSize;
    private Long tenantId;
    private CountDownLatch countDownLatch;
    private OrderIndexMapper orderIndexMapper;
    private OrderRepository orderRepository;
    private BaseOrderRepository baseOrderRepository;

    @Override
    public void run() {
        if (tenantId == null) {
            all();
        } else {
            tenant();
        }
    }

    public void all() {
        StopWatch watch = new StopWatch();
        watch.start();
        try {
            logger.info("订单数据迁移，页码:{}，开始", pageNum);
            // 分页查询订单数据
            var list = orderIndexMapper.getOrderIds(pageNum, pageSize);
            list.forEach(id -> {
                try {
                    var data = orderIndexMapper.getById(id);
                    if (Constants.ORDER_TENANT.contains(data.getIndexCreatorcompanyid())) {
                        logger.error("订单id：{},租户id:{}", id, data.getIndexCreatorcompanyid());
                        baseOrderRepository.sync(null);
                    } else {
                        orderRepository.process(data);
                        baseOrderRepository.sync(ScannerOrderAssembler.assemble(OrderConvert.convert(data)));
                    }
                } catch (Exception e) {
                    logger.error("订单同步异常：{},{}", id, e.getMessage());
                    e.printStackTrace();

                }
            });
            // 完成1个任务
            if (countDownLatch != null) {
                countDownLatch.countDown();
            }
            watch.stop();
            logger.info("订单数据迁移，页码:{}，结束，耗时:{}", pageNum, watch.getTotalTimeSeconds());
        } catch (Exception e) {
            logger.error("订单数据迁移,页码:{}，运行失败", pageNum, e);
            countDownLatch.countDown();
        }
    }

    public void tenant() {
        StopWatch watch = new StopWatch();
        watch.start();
        try {
            logger.info("租户{}订单数据迁移，页码:{}，开始", tenantId, pageNum);
            // 分页查询订单数据
            var list = orderIndexMapper.getOrderIdsByTenantId(pageNum, pageSize, tenantId);
            list.forEach(id -> {
                try {
                    var data = orderIndexMapper.getById(id);
                    orderRepository.process(data);
                    baseOrderRepository.sync(ScannerOrderAssembler.assemble(OrderConvert.convert(data)));
                } catch (Exception e) {
                    logger.error("订单同步异常：{},{}", id, e.getMessage());
                    e.printStackTrace();

                }
            });
            // 完成1个任务
            if (countDownLatch != null) {
                countDownLatch.countDown();
            }
            watch.stop();
            logger.info("租户{}订单数据迁移，页码:{}，结束，耗时:{}", tenantId, pageNum, watch.getTotalTimeSeconds());
        } catch (Exception e) {
            logger.error("租户{}订单数据迁移,页码:{}，运行失败", tenantId, pageNum, e);
            countDownLatch.countDown();
        }
    }
}
